<?php

require_once './php/core/p_pre.php';
process($db, $userId, $deviceId);
require_once './php/core/p_post.php';

function process(&$db, $userId, $deviceId) {
	require_once './php/core/dataset.php';
	
	$inkUrl = trim(param('data'));
	if ( strlen($inkUrl) < 1 ) {
		echo 'Missing parameter: data. <br/>Example: http://www.pathkey.org/key.php?data=http://flora.huh.harvard.edu/china/delta/Acer/Acer.ink';
		return;
	}
	
	$data = DataSet::findByInkUrl($db, $inkUrl);
	if ( !$data ) {
		$data = new DataSet();
		$data->insert($db, $inkUrl);
		$data = DataSet::findByInkUrl($db, $inkUrl);
	}
	
	if ( !$data ) {
		return;
	}

	if ( strlen($data->dataDir) < 1 ) {
		echo 'The data is not available at present.  Please try in a day or two. <br/>Sorry for the inconvenience. 
		<br/>Here is link to <a href="index.php">Contact Us</a>';
		return;
	}
	
	$keyPage;
	$version = param('version');
	if ( strlen($version) > 0) {
		if ( $version == 'm' ) {
			$keyPage = 'key_m.php';
		} else {
			$keyPage = 'key_d.php';
		}
	} else {
		if ( CROUtil::isShowMobileVersion() ) {
			$keyPage = 'key_m.php';
		} else {
			$keyPage = 'key_d.php';
		}
	}
	
	header('Location: '.$keyPage.'?datasetId='.$data->id);
}
?>

